package front

import (
	"github.com/juju/errors"
	"qihuang/db/mysql"
	"qihuang/loger"
	"qihuang/model"
)

type InvitationParams struct {
	CreateTime string `json:"create_time"`
	ModifyTime string `json:"modify_time"`
	Status     int    `json:"status"`
	Code       string `json:"code"`
}

//文章详情
type Invitation struct {
	model.Invitation
}

type Invitations struct {
}


//获取文章详情
func (c *Invitations) GetInvitation(code string) (status bool, inv model.Invitation, msg string) {

	//articleContent := model.ArticlesContents{}
	invitation := model.Invitation{}

	db := mysql.Default().GetConn()
	defer db.Close()
	if err := db.Where("code = ?", code).Where("status = ?", 0).First(&invitation).Error; err != nil {
		loger.Loger.Error(errors.ErrorStack(errors.Trace(err)))

		return false, invitation, "邀请码不存在"
	}
	if invitation.Id <= 0 {
		return false, invitation, "邀请码不存在"
	}

	return true, invitation, "查询成功"
}


func (c *Invitations) DeleteInvitation(invId int) error {
	db := mysql.Default().GetConn()
	defer db.Close()
	err := db.Model(&model.Invitation{}).Where("id = ?", invId).Update("status", "-1").Error
	tx := db.Begin()
	if err != nil {
		loger.Loger.Error(errors.ErrorStack(errors.Trace(err)))
		tx.Rollback()
		return err
	}
	//提交事务
	tx.Commit()
	return err
}
